// 频道编辑弹层内容组件
<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="mini"
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        v-for="(channel, index) in myChannels"
        :key="index"
        class="grid-item"
        @click="onChannelClick(channel, index)"
      >
        <span slot="text" class="text" :class="{ active: index === active }">
          {{ channel.name }}
        </span>
        <van-icon
          slot="icon"
          name="clear"
          v-if="isEdit && !fixedChannels.includes(channel.id)"
        />
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        icon="plus"
        class="grid-item"
        @click="addChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannels, delUserChannels } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
// console.log(getAllChannels)
// console.log(addUserChannels)
// console.log(delUserChannels)
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fixedChannels: [0],
    }
  },
  computed: {
    /* recommendChannels() {
      const channels = []
      this.allChannels.forEach(item => {
        // const flag = this.myChannels.find(val => val.id === item.id)
        const flag = this.myChannels.some(val => val.id === item.id)
        if (!flag) {
          channels.push(item)
        }
      })
      return channels
    }, */
    /* recommendChannels() {
      return this.allChannels.filter(
        item => !this.myChannels.some(val => val.id === item.id)
      )
    }, */
    recommendChannels() {
      return this.allChannels.filter(item =>
        this.myChannels.every(val => val.id !== item.id)
      )
    },
    ...mapState(['user']),
  },
  watch: {},
  created() {
    this.loadAllChannels()
  },
  mounted() {},
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels()
        // console.log(data)
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取频道数据失败!')
      }
    },
    async addChannel(channel) {
      // console.log(channel);
      this.myChannels.push(channel)
      // 数据持久化存储，判断是否处于登录状态
      // console.log(this.user)
      if (this.user) {
        try {
          await addUserChannels({
            id: channel.id,
            seq: this.myChannels.length,
          })
        } catch (err) {
          this.$toast('请求失败，请稍后重试！')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    onChannelClick(channel, index) {
      // console.log(channel + '-----' + index)
      if (this.isEdit) {
        // 编辑状态
        if (this.fixedChannels.includes(channel.id)) {
          return
        }
        this.myChannels.splice(index, 1)
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        this.deleteChannel(channel)
      } else {
        // 非编辑状态
        this.$emit('update-active', index, false)
      }
    },
    async deleteChannel(channel) {
      if (this.user) {
        // console.log(1)
        try {
          await delUserChannels(channel.id)
        } catch (err) {
          this.$toast('删除频道失败，请稍后重试！')
        }
      } else {
        // console.log(2)
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
  },
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
